﻿using FakeXieCheng.API.Dtos;
using FakeXieCheng.API.Services;
using FakeXieCheng.API.ResourceParameters;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using System.Text.RegularExpressions;

namespace FakeXieCheng.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TouristRoutesController : ControllerBase
    {
        private ITouristRouteRepository _touristRouteRespository;
        private readonly IMapper _mapper;
        public TouristRoutesController(
            ITouristRouteRepository touristRouteRepository,
            IMapper mapper
            )
        {
            _touristRouteRespository = touristRouteRepository;
            _mapper = mapper;
        }
        // GET: api/TouristRoutes?keyword=传入的参数
        [HttpGet]
        [HttpHead]
        public IActionResult GerTouristRoutes(
            [FromQuery]TouristRouteResourceParameters parameters
            //[FromQuery]string keyword,
            //string rating // 小于 lessThan2, 大于 largeThan3，等于 equalThan5
            )
        {
            
            var touristRoutesFromRepo = _touristRouteRespository.GetTouristRoutes(parameters.Keyword, parameters.RatingOperator, parameters.RatingValue);
            if(touristRoutesFromRepo == null || touristRoutesFromRepo.Count() <= 0)
            {
                return NotFound("没有旅游路线");
            }
            var touristRoutesDto = _mapper.Map<IEnumerable<TouristRouteDto>>(touristRoutesFromRepo);
            return Ok(touristRoutesDto);
        }

        // GET api/<TestAPIController>/5
        [HttpGet("{id}")]
        public IActionResult GetTouristRoute(Guid id)
        {
            var touristRouteFromRepo = _touristRouteRespository.GetTouristRoute(id);
            if (touristRouteFromRepo == null)
            {
                return NotFound($"旅游路线{id}未找到");
            }
            // dto 手动引入
            //var touristRouteDto = new TouristRouteDto()
            //{
            //    Id = touristRouteFromRepo.Id,
            //    Title = touristRouteFromRepo.Title,
            //    Description=touristRouteFromRepo.Description,
            //    Price=touristRouteFromRepo.OriginalPrice * (decimal)(touristRouteFromRepo.DiscountPresent ?? 1),
            //    CreateTime = touristRouteFromRepo.CreateTime,
            //    UpdateTime = touristRouteFromRepo.UpdateTime,
            //    Features = touristRouteFromRepo.Features,
            //    Notes = touristRouteFromRepo.Notes,
            //    Rating = touristRouteFromRepo.Rating,
            //    TravelDays = touristRouteFromRepo.TravelDays.ToString(),
            //    TripType = touristRouteFromRepo.TripType.ToString(),
            //    DepartureCity = touristRouteFromRepo.DepartureCity.ToString(),

            //};

            //通过 Automapper
            var touristRouteDto = _mapper.Map<TouristRouteDto>(touristRouteFromRepo);

            //return Ok(touristRouteFromRepo);

            return Ok(touristRouteDto);
        }
    }
}
